/******************************************************************************
* File description: CustomRadioButton.qml
* Function: 自定义单选按钮
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-30
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Controls.impl 2.14
import QtQuick.Layouts 1.14
import SvgImage 1.0

RadioButton {
    id: root

    /* property 定义开始 */
    property string bgColor: "transparent"
    property int radius: -1
    property int indicatorWidth: 15
    property int indicatorHeight: 15
    property url indicatorPath: ""
    property string radioColor: "#FF2F42CD"
    property string textColor: "#FF222222"
    property int fontPixelSize: 16
    /* property 定义结束 */

    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
                            implicitContentWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             implicitContentHeight + topPadding + bottomPadding,
                             implicitIndicatorHeight + topPadding + bottomPadding)

    // 间隔
    spacing: 9

    // 背景
    background: Rectangle {
        color: root.bgColor
    }

    // 指示器
    indicator: Rectangle {
        implicitWidth: root.indicatorWidth
        implicitHeight: root.indicatorHeight

        x: text
           ? (root.mirrored
              ? root.width - width - root.rightPadding
              : root.leftPadding)
           : root.leftPadding + (root.availableWidth - width)/2
        y: root.topPadding + (root.availableHeight - height)/2

        radius: root.radius<0 ? width/2 : root.radius
        color: "transparent"
        border.width: 1
        border.color: root.radioColor

        Rectangle {
            anchors.fill: parent
            anchors.margins: 5
            radius: root.radius<0 ? width/2 : root.radius
            color: parent.border.color
            visible: root.checked
        }
    }

    // 内容
    contentItem: Label {
        leftPadding: root.indicator && !root.mirrored
                     ? root.indicator.width + root.spacing
                     : 0
        rightPadding: root.indicator && root.mirrored
                      ? root.indicator.width + root.spacing
                      : 0
        text: root.text
        font.pixelSize: root.fontPixelSize
        color: root.textColor
        renderType: Text.NativeRendering
    }
}
